<?php

namespace App\Api\Controllers;
use Log;
use App\Driver;
use App\Api\Controllers\BaseController;
use Illuminate\Support\Facades\Session;
use Curl\Curl;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Foundation\Testing\TestCase;
use App\Http\Requests;
use App\Wechat;
use JWTAuth;
use TopClient;
use ResultSet;
use DB;
use RequestCheckUtil;
use TopLogger;
use Illuminate\Support\Facades\Redis;
use AlibabaAliqinFcSmsNumSendRequest;

class WechatTestController extends BaseController
{

    public function __construct(){
        parent::__construct();
    }
  
	public function testinfo(Request $request)
     {
	    $code = $request->input('code');
	    $tel = $request->input('tel');
	    $password = $request->input('password');
		
		$appid = "wx95eaefe010f7c7e8";
      		$appsecret = "bf5f7646164df5cd99d8a0363b6b0ed6";
      		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
      		$curl = new Curl();
      		$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
      		$curl->get($url);
      		$response = $curl->response;
      		$response = json_decode($response,true);
	 if(isSet($response['openid'])){
      		$access_token = $response['access_token'];
      		$openid = $response['openid'];
      		$url_info ="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
      		$curl->get($url_info);
      		$info = $curl->response;
      		$info = json_decode($info,true);
	
      if($last = Driver::where('driverPhone','=',$tel)->first()) {
        if (sha1($password) == $last['password']) {
	   if(DB::table('drivers_openid')->where('openid','=',$openid)->orWhere('driverid', $tel)->first()){
		return $this->returnMsg('50004','existed','');
	   }
		DB::table('drivers_openid')->insert(['driverid'=>$tel,'openid'=>$openid]);
		return $this->returnMsg('200','pass avalidator',$info);
	}
	else return $this->returnMsg('50001','wrong password',$tel);
	}
	else return $this->returnMsg('50002','Not found tel');
		
	}	    
	else return $this->returnMsg('50003','can not get the  openid',$code);
}
}

